package cn.gok;
//1 创建类 School类：name,address,  birthYear成立的年份：int （2002；2010）
//        实现序列化接口，void writeToFile();//采用对象流写入将两个school对象写入到 1.dat中，
//        void readFromFile();//从1.dat中读取数据到对象，并显示出来

import java.io.*;

class School implements Serializable {
    private String name;
    private String address;
    private int birthYear;

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "student{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }
}

public class homework5 {
    public static void main(String[] args) throws IOException {

        writeToFile();
        readFromFile();
    }

    private static void readFromFile() throws IOException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("C:\\Users\\user\\Desktop\\resp1\\day5\\src\\cn\\gok\\homework\\1.dat"));
        try {
            while (true) {
                Object obj = ois.readObject();
                System.out.println(obj);
            }
        }catch (IOException | ClassNotFoundException e){
            System.out.println("文件读取结束");
        }
    }

    private static void writeToFile() throws IOException {

        School school = new School("asd","sad",2002);
        School school2 = new School("cxf","asd",2010);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\user\\Desktop\\resp1\\day5\\src\\cn\\gok\\homework\\1.dat"));
        oos.writeObject(school);
        oos.writeObject(school2);
        oos.close();
        System.out.println("写入成功");
    }
}
